import time
from random import Random

from rest_framework import generics, mixins, status
from rest_framework.permissions import IsAuthenticated
# JWT
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from rest_framework.response import Response

from utils.permissions import IsOwnerOrReadOnly
from .models import ShoppingCart, OrderInfo, OrderGoods
from .serializers import ShoppingCartSerializer, ShoppingCartListSerializer, OrderCreateSerializer, OrderListSerializer


class ShoppingCartAPIView(generics.GenericAPIView, mixins.CreateModelMixin, mixins.ListModelMixin):
    """
    购物车
    get:
    获取购物车列表
    post:
    新增购物车
    """
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly,)  # 限制权限，只有登录的用户才可以调用，IsOwnerOrReadOnly只能访问自己相关的数据
    authentication_classes = (JSONWebTokenAuthentication,)  # 配置验证使用 JWT 验证
    # serializer_class = ShoppingCartSerializer
    lookup_field = "goods_id"

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

    def get_queryset(self):
        return ShoppingCart.objects.filter(user=self.request.user)

    def get_serializer_class(self):
        if self.request is None:
            return ShoppingCartListSerializer
        if self.request.method == 'GET':
            return ShoppingCartListSerializer
        else:
            return ShoppingCartSerializer


class ShoppingCartModifyAPIView(generics.GenericAPIView, mixins.UpdateModelMixin):
    """
    购物车操作
    post:
    更新商品数量
    """
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly,)  # 限制权限，只有登录的用户才可以调用，IsOwnerOrReadOnly只能访问自己相关的数据
    authentication_classes = (JSONWebTokenAuthentication,)  # 配置验证使用 JWT 验证
    serializer_class = ShoppingCartSerializer
    lookup_field = "goods_id"

    def post(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def get_queryset(self):
        return ShoppingCart.objects.filter(user=self.request.user)


class ShoppingCartDeleteAPIView(generics.GenericAPIView, mixins.DestroyModelMixin):
    """
    购物车操作
    post:
    更新商品数量
    """
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly,)  # 限制权限，只有登录的用户才可以调用，IsOwnerOrReadOnly只能访问自己相关的数据
    authentication_classes = (JSONWebTokenAuthentication,)  # 配置验证使用 JWT 验证
    serializer_class = ShoppingCartSerializer
    lookup_field = "goods_id"

    def post(self, request, *args, **kwargs):
        return self.destroy(request, *args, **kwargs)

    def get_queryset(self):
        return ShoppingCart.objects.filter(user=self.request.user)


class OrderCreateAPIView(generics.GenericAPIView, mixins.CreateModelMixin):
    """
    订单列表
    post:
    创建订单
    """
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly,)  # 限制权限，只有登录的用户才可以调用，IsOwnerOrReadOnly只能访问自己相关的数据
    authentication_classes = (JSONWebTokenAuthentication,)  # 配置验证使用 JWT 验证
    serializer_class = OrderCreateSerializer

    def post(self, request, *args, **kwargs):
        return self.create(request, *args, **kwargs)

    def get_queryset(self):
        return OrderInfo.objects.filter(user=self.request.user)

    def perform_create(self, serializer):
        # 在 create 之前调用
        order = serializer.save()
        shop_carts = ShoppingCart.objects.filter(user=self.request.user)
        # 将购物车中的商品转存成为订单商品
        for shop_cart in shop_carts:
            order_goods = OrderGoods()
            order_goods.goods = shop_cart.goods
            order_goods.goods_num = shop_cart.nums
            order_goods.order = order
            order_goods.save()
            shop_cart.delete()  # 删除原来购物车中的数据
        return order


class OrderListAPIView(generics.GenericAPIView, mixins.ListModelMixin, mixins.RetrieveModelMixin):
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly,)  # 限制权限，只有登录的用户才可以调用，IsOwnerOrReadOnly只能访问自己相关的数据
    authentication_classes = (JSONWebTokenAuthentication,)  # 配置验证使用 JWT 验证
    serializer_class = OrderListSerializer

    def get(self, request, *args, **kwargs):
        return self.list(request, *args, **kwargs)

    def get_queryset(self):
        return OrderInfo.objects.filter(user=self.request.user)


class OrderDetailAPIView(generics.GenericAPIView, mixins.RetrieveModelMixin):
    permission_classes = (IsAuthenticated, IsOwnerOrReadOnly,)  # 限制权限，只有登录的用户才可以调用，IsOwnerOrReadOnly只能访问自己相关的数据
    authentication_classes = (JSONWebTokenAuthentication,)  # 配置验证使用 JWT 验证
    serializer_class = OrderListSerializer

    def get(self, request, *args, **kwargs):
        return self.retrieve(request, *args, **kwargs)

    def get_queryset(self):
        return OrderInfo.objects.filter(user=self.request.user)